📦 Maven Notes (Complete Guide for Spring Boot Developers)


1️⃣ What is Maven?

Maven is a build automation and dependency management tool primarily used for Java projects.

It helps you:


2️⃣ Why Maven is Important in Spring Boot?

Spring Boot projects rely heavily on external libraries like:

Without Maven, you would manually:

Maven automates all of this.


3️⃣ Maven Project Structure (Standard Layout)

my-project/
│
├── pom.xml
│
└── src/
    ├── main/
    │   ├── java/
    │   └── resources/
    │
    └── test/
        └── java/

4️⃣ Maven Repositories

• Local Repository → .m2 folder in your system
• Central Repository → Default online repo
• Remote Repository → Company/private repo

🔹 Local Repository

Default Location:

C:\Users\YourName\.m2\repository

Stores downloaded dependencies locally.

🔹 Central Repository

https://repo.maven.apache.org/maven2

Official public repository maintained by Maven.

🔹 Remote Repository

Used in companies (Nexus / Artifactory) to host private/internal dependencies.


5️⃣ Maven Lifecycle

Maven follows a predefined lifecycle for building projects.

🔁 Default Lifecycle Phases

validate
   ↓
compile
   ↓
test
   ↓
package
   ↓
verify
   ↓
install
   ↓
deploy

📌 Explanation of Important Phases

Common Commands

Command Purpose
mvn clean Deletes target folder
mvn compile Compiles source code
mvn test Runs tests
mvn package Creates JAR/WAR
mvn install Installs in local repo
mvn clean install Clean + full build

6️⃣ Basic pom.xml Structure

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo-app</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

</project>

7️⃣ Dependencies Example

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

</dependencies>

8️⃣ Spring Boot Maven Plugin

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Run application:

mvn spring-boot:run

✅ Summary

Maven is:

Essential for Spring Boot project management and builds.